package com.zengqingfa.zookeeper.demo.features;

import java.util.concurrent.CountDownLatch;

public class TicketSeller implements Runnable {

    static int ticketNum = 1;

    static int NUM = 5;

    static CountDownLatch countDownLatch = new CountDownLatch(NUM);

    public void run() {
        System.out.println("售票开始");
        try {
            MyLock lock = new MyLock();
            countDownLatch.await();
            // 获取锁
            lock.acquireLock();
            if (ticketNum > 0) {
                Thread.sleep(1000);
                ticketNum--;
                System.out.println("当前票为：" + ticketNum);
            } else {
                System.out.println("票已卖完");
            }
            //释放锁
            lock.releaseLock();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        for (int i = 0; i < NUM; i++) {
            new Thread(new TicketSeller()).start();
            countDownLatch.countDown();
        }
    }
}
